package io.renren.sf.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.dao.CbExpandFileDao;
import io.renren.sf.dao.ExpandResouceVoDao;
import io.renren.sf.entity.CbExpandFileEntity;
import io.renren.sf.vo.ExpandResouceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.sf.dao.CbExpandResourceDao;
import io.renren.sf.entity.CbExpandResourceEntity;
import io.renren.sf.service.CbExpandResourceService;
import org.springframework.util.StringUtils;


@Service("cbExpandResourceService")
public class CbExpandResourceServiceImpl extends ServiceImpl<CbExpandResourceDao, CbExpandResourceEntity> implements CbExpandResourceService {

    @Autowired
    private CbExpandFileDao cbExpandFileDao;

    @Autowired
    private ExpandResouceVoDao expandResouceVoDao;

    @Override
    public Page<CbExpandResourceEntity> getExpandResource(Map<String, Object> params) {
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        QueryWrapper<CbExpandResourceEntity> queryWrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        String type = (String) params.get("type");
        String addType = (String) params.get("addType");
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        if(!StringUtils.isEmpty(type)){
            queryWrapper.eq("expand_edition_type", Integer.parseInt(type));
        }
        if(!StringUtils.isEmpty(addType)){
            queryWrapper.eq("add_type", Integer.parseInt(addType));
        }
        Page<CbExpandResourceEntity> expandResourcePage = new Page<CbExpandResourceEntity>(page, limit);
        baseMapper.selectPage(expandResourcePage, queryWrapper);
        for (CbExpandResourceEntity record : expandResourcePage.getRecords()) {
            record.setChildren(cbExpandFileDao.selectList(new QueryWrapper<CbExpandFileEntity>().eq("rid", record.getId())));
        }
//        expandResourcePage.setTotal(expandResouceVoDao.getExpandResourceTotal(queryWrapper));
        return expandResourcePage;
    }
}